package com.tyust.web;

import java.io.IOException;
import java.sql.Connection;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.transform.Result;

import com.tyust.dao.GradeDao;
import com.tyust.dao.StudentDao;
import com.tyust.util.DbUtil;
import com.tyust.util.ResponseUtil;

import net.sf.json.JSONObject;

public class GradeDeleteServlet extends HttpServlet{

	DbUtil dbUtil = new DbUtil();
	GradeDao gradeDao = new GradeDao();
	StudentDao studentDao = new StudentDao();
	@Override
	public void service(HttpServletRequest request, 
			HttpServletResponse response) 
					throws ServletException, IOException {
		String delIds = request.getParameter("delIds");
		Connection con = null;
		try {
			con = dbUtil.getCon();
			String str[] = delIds.split(",");
			JSONObject result = new JSONObject();
			for(int i=0;i<str.length;i++){
				boolean f = studentDao.getStudentByGradeId(con, str[i]);
				if(f){
					result.put("errorIndex", i);
					result.put("errorMsg", "班级有学生，不能删除！");
					ResponseUtil.write(response, result);
					return;
				}
			}
			int delNums = gradeDao.gradeDelete(con, delIds);
			if(delNums>0){
				result.put("success", "true");
				result.put("delNums", delNums);
			}else{
				result.put("errorMsg", "删除失败！");
			}
			ResponseUtil.write(response, result);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				dbUtil.closeCon(con);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
